import {reqGoodsInfo, reqAddOrUpdateShopCart} from '@/api'
import {getUUID} from '@/utils/uuid_token.js'

const namespaced = true
const state = {
  goodInfo: {},
  uuid: getUUID()
}

const actions = {
  async getGoodInfo({commit},skuId){
    let result= await reqGoodsInfo(skuId)
    if ( result.code == 200) {
      commit('GETGOODINFO', result.data)
    }
  },
  // 添加购物车
  async addOrUpdateShopCart({commit}, {skuId, skuNum}){
     let result =  await reqAddOrUpdateShopCart(skuId, skuNum)
     if (result.code === 200) {
       // 需要返回一个值 为Promise状态
       return 'ok'
     }else{
       return Promise.reject(new Error('失败了'))
     }
  }
}

const mutations = {
  GETGOODINFO(state, good){
    state.goodInfo = good
  }
}

const getters = {
  categoryView(state){
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state){
    return state.goodInfo.skuInfo || {}
  },
  // 产品售卖 属性
  spuSaleAttrList(state){
    return state.goodInfo.spuSaleAttrList || []
  }
}

export default {
  namespaced,
  state,
  actions,
  mutations,
  getters
}